//package com.main.gateway.listener;
//
//import org.apache.hc.client5.http.classic.methods.HttpPost;
//import org.apache.http.client.methods.CloseableHttpResponse;
//import org.apache.http.entity.StringEntity;
//import org.apache.http.impl.client.CloseableHttpClient;
//import org.apache.http.impl.client.HttpClients;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.cloud.gateway.event.RefreshRoutesEvent;
//import org.springframework.cloud.gateway.filter.factory.CacheRequestBodyGatewayFilterFactory;
//import org.springframework.context.ApplicationListener;
//import org.springframework.context.annotation.Bean;
//import org.springframework.http.*;
//import org.springframework.stereotype.Component;
//import org.springframework.web.client.RestTemplate;
//import org.springframework.web.reactive.function.client.WebClient;
//import reactor.core.publisher.Mono;
//
//import java.io.BufferedReader;
//import java.io.DataOutputStream;
//import java.io.IOException;
//import java.io.InputStreamReader;
//import java.net.HttpURLConnection;
//import java.net.URL;
//import java.nio.charset.StandardCharsets;
//import java.util.Base64;
//import java.util.HashMap;
//import java.util.Map;
//
//import static org.apache.logging.log4j.message.MapMessage.MapFormat.JSON;
//
//@Component
//public class GatewayRoutesRefreshListener implements ApplicationListener<RefreshRoutesEvent> {
//    private final CacheRequestBodyGatewayFilterFactory requestBody;
//    @Value("${server.address}")
//    private String address;
//    @Value("${server.http.port}")
//    private String port;
//
//    public GatewayRoutesRefreshListener(CacheRequestBodyGatewayFilterFactory requestBody) {
//        this.requestBody = requestBody;
//    }
//
//    public String callExternalApiUsingHttpURLConnection() throws IOException {
//        URL url = new URL("http://"+address+":"+ port +"/actuator/gateway/refresh");
//        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
//        conn.setRequestMethod("POST");
//
//        BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
//        String inputLine;
//        StringBuilder content = new StringBuilder();
//
//        while ((inputLine = in.readLine()) != null) {
//            content.append(inputLine);
//        }
//
//        in.close();
//        return content.toString();
//    }
//
//    public void doPost() throws IOException {
//        //创建URL对象
//        URL url = new URL("http://"+address+":"+ port +"/actuator/gateway/refresh");
//        //打开连接
//        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
//        //设置请求方式
//        conn.setRequestMethod("POST");
//        // 设置请求头
//        conn.setRequestProperty("Content-Type", "application/json");
//        //启用输出流
//        conn.setDoOutput(true);
//        //设置请求体数据
//        Map map = new HashMap<>();
//        map.put("name", "user");
//        String requestBody = map.toString();
//        //发送请求体数据
//        try (DataOutputStream outputStream = new DataOutputStream(conn.getOutputStream())) {
//            outputStream.write(requestBody.getBytes(StandardCharsets.UTF_8));
//        }
//        //读取响应内容
//        BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
//        StringBuilder sb = new StringBuilder();
//        String line;
//        while ((line = reader.readLine()) != null) {
//            sb.append(line);
//        }
//        reader.close();
//        System.out.println(sb.toString());
//    }
//    @Override
//    public void onApplicationEvent(RefreshRoutesEvent event) {
////        String url =  "http://"+address+"/:"+ port +"/actuator/gateway/refresh";
//        String username = "user";
//        String password = "pass";
//        // 当接收到事件时，Gateway会自动刷新其路由规则
//        System.out.println("Gateway routes refreshed.");
//        try {
//            doPost();
//        } catch (IOException e) {
//            throw new RuntimeException(e);
//        }
//    }
//}